﻿using GN.Data.Entity.Attribute;
using System;
using System.Collections.Generic;
using System.Text;
using WxOpenPLatCore.DBContext;
using WxOpenPLatCore.Interfaces;
using WxOpenPLatCore.Models;

namespace WxOpenPLatCore.Services
{
    public class DomainsService : IService
    {
        public List<Domains> Query()
        {
            return DbSqlHelper.GetEntities<Domains>("select * from open_domains", new MySql.Data.MySqlClient.MySqlParameter[0]);
        }

        public Domains GetById(int id)
        {
            return DbSqlHelper.GetEntity<Domains>("select * from open_domains where id=@id", new MySql.Data.MySqlClient.MySqlParameter[] { new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@id", Value = id } });
        }

        public Domains GetByHost(string host)
        {
            return DbSqlHelper.GetEntity<Domains>("select * from open_domains where host=@host", new MySql.Data.MySqlClient.MySqlParameter[] { new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@host", Value = host } });
        }

        public Domains GetByState(string state)
        {
            return DbSqlHelper.GetEntity<Domains>("select * from open_domains where state=@state", new MySql.Data.MySqlClient.MySqlParameter[] { new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@state", Value = state } });
        }

        public bool Delete(string ids)
        {
            string[] _ids = ids.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            List<int> qids = new List<int>();
            foreach (string item in _ids)
            {
                int _id = int.Parse(item);
                qids.Add(_id);
            }
            if (qids.Count == 0)
            {
                return false;
            }
            int ret = DbSqlHelper.ExecuteNonQuery("delete from open_domains where id in (@ids)", new MySql.Data.MySqlClient.MySqlParameter[] { new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@ids", Value = string.Join(",", qids) } });
            return ret > 0;
        }

        public bool AddOrUpdate(Domains model)
        {
            System.Reflection.PropertyInfo[] info = typeof(Domains).GetProperties();
            StringBuilder fields = new StringBuilder();
            StringBuilder values = new StringBuilder();
            List<MySql.Data.MySqlClient.MySqlParameter> parameters = new List<MySql.Data.MySqlClient.MySqlParameter>();
            StringBuilder strSql = new StringBuilder();
            StringBuilder whereid = new StringBuilder();
            if (model.id == 0)
            {
                model.adddate = DateTime.Now;
                foreach (var property in info)
                {
                    Identiy[] identitys = (Identiy[])property.GetCustomAttributes(typeof(Identiy), false);
                    NoField[] nofield = (NoField[])property.GetCustomAttributes(typeof(NoField), false);
                    if (identitys.Length == 0 && nofield.Length == 0)
                    {
                        object value = property.GetValue(model, null);
                        fields.Append(property.Name + ",");
                        values.Append("@" + property.Name + ",");
                        parameters.Add(new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@" + property.Name, Value = value });
                    }
                }
                fields.Remove(fields.Length - 1, 1);
                values.Remove(values.Length - 1, 1);
                
                strSql.AppendFormat("insert into {0}(", "open_Domains");
                strSql.Append(fields.ToString() + ")");
                strSql.Append(" values (");
                strSql.Append(values.ToString() + ")");
                strSql.Append(";select @@IDENTITY");
                object obj = DbSqlHelper.ExecuteScalar(strSql.ToString(), parameters.ToArray());
                return (UInt64)obj > 0;
            }
            else
            {
                foreach (var property in info)
                {
                    Identiy[] identitys = (Identiy[])property.GetCustomAttributes(typeof(Identiy), false);
                    NoField[] nofield = (NoField[])property.GetCustomAttributes(typeof(NoField), false);
                    if (identitys.Length == 0 && nofield.Length == 0)
                    {
                        object val = property.GetValue(model, null);
                        fields.Append(property.Name + "=@" + property.Name + ",");
                        parameters.Add(new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@" + property.Name, Value = val });
                    }
                    else if (identitys.Length > 0)
                    {
                        object val = property.GetValue(model, null);
                        whereid.Append(property.Name + "=@" + property.Name);
                        parameters.Add(new MySql.Data.MySqlClient.MySqlParameter() { ParameterName = "@" + property.Name, Value = val });
                    }
                }
                fields.Remove(fields.Length - 1, 1);
                strSql.AppendFormat("update {0} set {1}", "open_Domains", fields.ToString());
                strSql.Append(" where " + whereid.ToString());
                int obj = DbSqlHelper.ExecuteNonQuery(strSql.ToString(), parameters.ToArray());
                return obj > 0;
            }
        }
    }
}
